package com.changgou.service.goods.controller;

import com.changgou.common.pojo.PageResult;
import com.changgou.common.pojo.Result;
import com.changgou.common.pojo.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.service.goods.service.BrandService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.security.PublicKey;
import java.util.List;
import java.util.Map;

@RequestMapping("/brand")
@RestController
@CrossOrigin//对当前控制层下的所有方法，都开启跨域请求
public class BrandController {

	@Autowired
	BrandService brandService;

	//1、返回品牌表
	@GetMapping("/findBrandList")
	@ResponseBody
	public Result<List<Brand>> findList(){
		List<Brand> brandList = brandService.findList();
		return new Result<>(true, StatusCode.OK,"查询品牌列表成功",brandList);
	}

	//2、根据id查询品牌信息
	@GetMapping("/findBrandById/{id}")  //  /findBrandById可加可不加
	@ResponseBody
	public Result<Brand> findBrandById(@PathVariable("id") Integer id){
		Brand brand = brandService.findBrandById(id);
		return new Result<>(true,StatusCode.OK,"根据品牌id，查询品牌信息成功",brand);
	}

	//3、添加品牌
	@PostMapping("/addBrand")
	public void addBrand(@RequestBody Brand brand){
		brandService.addBrand(brand);
	}

	//4、修改品牌信息
	@PostMapping("/updateBrand/{id}")
	public void update(@PathVariable("id") Integer id,@RequestBody Brand brand){
		brand.setId(id);
		brandService.updateBrand(brand);
	}

	//5、根据id，删除某个品牌信息
	@DeleteMapping("/deById/{id}")
	public Result delById(@PathVariable("id") Integer id){
		brandService.delById(id);
		return new Result(true,StatusCode.OK,"删除成功");
	}

	//6、根据条件查询（品牌（name）模糊，字母（letter）精确）
	@GetMapping("/search")
	public Result<List<Brand>> search(@RequestParam Map map){///这里的Map<String,Object>可以换为Map
		List<Brand> brandList = brandService.search(map);
		return new Result<>(true,StatusCode.OK,"查询成功",brandList);
	}

	//7、根据 当前页 和 每页展示条数，获取品牌信息（分页查询所有品牌）
	@GetMapping("/searchAllAndPage/{pageNum}/{pageSize}")
	public Result searchAllAndPage(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
		Page<Brand> pageInfo = brandService.searchAllAndPage(pageNum, pageSize);
		long total = pageInfo.getTotal();
		List<Brand> result = pageInfo.getResult();
		PageResult pageResult=new PageResult(total,result);
		return new Result(true,StatusCode.OK,"查询所有品牌，并分页成功",pageResult);
	}


	//8、分页+查询
	@GetMapping("/searchAndPage/{pageNum}/{pageSize}")
	public Result searchAndPage ( @RequestParam Map<String, Object> map, @PathVariable("pageNum")Integer pageNum, @PathVariable("pageSize")Integer pageSize){
		//注意：map前面加@RequestParam

		//手动制造异常，用于测试统一异常类
		//int i=1/0;

		Page<Brand> pageInfo = brandService.searchAndPage(map, pageNum, pageSize);
		long total = pageInfo.getTotal();
		List<Brand> result = pageInfo.getResult();
		PageResult pageResult=new PageResult(total,result);

		return new Result(true,StatusCode.OK,"查询某品牌，并分页成功",pageResult);
	}

}
